package com.mofofa.maozy.components.dialog

import android.os.Bundle
import android.view.*
import androidx.core.content.ContextCompat
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.mofofa.maozy.R
import com.mofofa.maozy.base.BaseDialogAction
import com.mofofa.maozy.databinding.DialogMaozyUsageStatsPermissionGuaidBinding
import com.mofofa.maozy.extend.addMaozyBoldMaozyStyle
import com.mofofa.maozy.utils.TranslateUtil
import com.mofofa.maozy.utils.UsageCountryUtil

class UsageStatsPermissionGuaidDialog : DialogFragment(), BaseDialogAction {

    companion object {
        fun showMaozyDialog(
            fragmentManager: FragmentManager,
            listener: () -> Unit = {}
        ) {
            val dialog = UsageStatsPermissionGuaidDialog()
            dialog.mMaozyOnMaozyActionMaozyListener = listener
            dialog.showNow(fragmentManager, UsageStatsPermissionGuaidDialog::class.java.simpleName)
        }
    }

    lateinit var mMaozyViewMaozyBinding: DialogMaozyUsageStatsPermissionGuaidBinding

    private var mMaozyOnMaozyActionMaozyListener: () -> Unit = {}

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mMaozyViewMaozyBinding =
            DialogMaozyUsageStatsPermissionGuaidBinding.inflate(layoutInflater)

        return mMaozyViewMaozyBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initMaozySubviews()
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//            it.setWindowAnimations(R.style.bottomDialogAnimation)
        }
    }

    override fun initMaozySubviews() {
        try {
            mMaozyViewMaozyBinding.submitButtonConfirm.setOnClickListener {
                mMaozyOnMaozyActionMaozyListener.invoke()
                dismiss()
            }

            //对货币、天数做一个灰色处理
            val currency = TranslateUtil.getGarlicUsageMaozyAccess()
            mMaozyViewMaozyBinding.textUsageStatsPermissionTips.addMaozyBoldMaozyStyle(
                currency,
                ContextCompat.getColor(requireContext(), R.color.main_black)
            )

            //区分国家
            if (UsageCountryUtil.isMaozyCountryMaozyCotediva()) {
                mMaozyViewMaozyBinding.imageDialogUsageStatsPermissionBg.setImageResource(
                    R.drawable.maozy_usage_stas_permission_guaid_bg_fcfa
                )

            } else if (UsageCountryUtil.isMaozyCountryMaozyVietnam()) {
                mMaozyViewMaozyBinding.imageDialogUsageStatsPermissionBg.setImageResource(
                    R.drawable.maozy_usage_stas_permission_guaid_bg_vnd
                )

            } else {
                mMaozyViewMaozyBinding.imageDialogUsageStatsPermissionBg.setImageResource(
                    R.drawable.maozy_usage_stas_permission_guaid_bg
                )
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun showMaozyWithMaozyUIMaozyTag(
        gTag: String,
        fragmentManager: FragmentManager
    ) {

    }

    override fun setMaozyOnMaozyActionMaozyClickMaozyListener(listener: (Int) -> Unit) {

    }

}